package mi.payment.transaction.refund;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.constants.Params;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.BaseScanActivity;
import sunmi.payment.L3.L3Handler;

/**
 * 退货模块 -- 二维码扫码界面
 *
 * @author longx on 2017/3/26.
 */
public class ReturnGoodsScanActivity extends BaseScanActivity {

    private static final String EXTRA_PAY_TYPE = "extra_pay_type";

    private int mPayChannel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        findViewById(R.id.prompt_text).setVisibility(View.GONE);

        mPayChannel = getIntent().getIntExtra(EXTRA_PAY_TYPE, 1);
    }

    @Override
    protected String getScanHintText() {
        return getString(R.string.please_scan_print_qr);
    }

    @Override
    protected String getClickText() {
        return getString(R.string.manual_input) + Configs.getInstance().RETURN_GOODS_INDEX_NAME();
    }

    @Override
    protected String getBarTitle() {
        return getString(R.string.return_title);
    }

    @Override
    protected View.OnClickListener getOnClickListener() {
        return v -> {
            QRCodeReturnGoodsActivity.startAction(ReturnGoodsScanActivity.this, mPayChannel, "");
            finish();
        };
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onScanSuccess(String data) {
        if (L3Handler.isL3Process()) {
            Params.sL3Params.oriQROrderNo = data;
        }

        //UI的判断是 当前选择的退货支付类型；0位银行卡，1位支付宝，2位微信，3银联扫码 5，合并二维码
        //而PayDetail的类型是 支持的交易平台: 0：银行卡；1：支付宝；2:微信;3:口碑；4:银联钱包 5:小米钱包
        //如下要做兼容
        //根据扫码得到的第三方订单号 查库 如果有则将选择的支付类型更新为库中的原始支付类型
        if (!TextUtils.isEmpty(data)) {
            PayDetail mPayDetail = mPayDetailModel.getPayDetailByThirdTransNum(data);
            if (mPayDetail != null) {
                mPayChannel = mPayDetail.transPlatform;
                if (mPayDetail.transPlatform == 4) {
                    mPayChannel = 3;
                } else if (mPayDetail.transPlatform == 5) {
                    mPayChannel = 5;
                }
            }
        }

        QRCodeReturnGoodsActivity.startAction(this, mPayChannel, data);
        finish();
    }

    public static void startAction(Context context, int type) {
        Intent intent = new Intent(context, ReturnGoodsScanActivity.class);
        intent.putExtra(EXTRA_PAY_TYPE, type);
        context.startActivity(intent);
    }


}
